package gui.mainWindow.finals;

import gui.mainWindow.MainWindow;

import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.SwingUtilities;

import tournamentTree.TournamentNode;

import com.mxgraph.model.mxCell;

public class FinalsMouseListener implements MouseListener {
	MainWindow window;

	public FinalsMouseListener(MainWindow window) {
		this.window = window;
	}

	@Override
	public void mouseClicked(MouseEvent e) {

		/* the mxCell clicked at */
		final Object selected = this.window.getGraphComponent().getCellAt(e.getX(), e.getY());
		if (selected != null) {
			if (selected instanceof mxCell) {

				mxCell tmp = (mxCell) selected; // the selected cell

				/* value must not be null */
				if (tmp.getValue() != null) {
					final TournamentNode tmpNode = (TournamentNode) tmp.getValue();

					if (tmpNode.getMatchData().isSelectable()) {
						SwingUtilities.invokeLater(new Runnable() {
							public void run() {
								FinalsEntryWindow inst = new FinalsEntryWindow(FinalsMouseListener.this.window, (mxCell) selected);
								inst.setLocationRelativeTo(null);
								inst.setVisible(true);
							}
						});
					}
				}
			}
		}
	}

	@Override
	public void mouseEntered(MouseEvent arg0) {
		// TODO Auto-generated method stub

	}

	@Override
	public void mouseExited(MouseEvent arg0) {
		// TODO Auto-generated method stub

	}

	@Override
	public void mousePressed(MouseEvent arg0) {
		// TODO Auto-generated method stub

	}

	@Override
	public void mouseReleased(MouseEvent e) {

	}

}
